<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 * @author   howe <howe1995@163.com>
 */

namespace App\Controller\Api\V1;

use App\Middleware\JWTAuthMiddleware;
use App\Middleware\MessageMiddleware;
use App\Services\MessageService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\RequestMapping;

#[Middlewares([JWTAuthMiddleware::class])]
#[Controller(prefix: '/api/v1/message/')]
class MessageController extends CController
{
    #[Inject]
    protected MessageService $messageService;

    /**
     * 聊天消息列表.
     */
    #[RequestMapping(path: 'list', methods: 'get')]
    public function list()
    {
        $params = $this->request->all();
        $this->validate($params, [
            'to_user' => 'required',
            'is_group' => 'required',
        ]);
        $data = $this->messageService->list($params);
        return $this->success($data, $this->request);
    }

    /**
     * 发送消息.
     */
    #[Middlewares([MessageMiddleware::class])]
    #[RequestMapping(path: 'send', methods: 'post')]
    public function send()
    {
        $params = $this->request->all();
        $this->validate($params, [
            'to_user' => 'required',
            'content' => 'required',
            'type' => 'required',
            'is_group' => 'required',
        ]);
        $data = $this->messageService->send($params);
        return $this->success($data, $this->request);
    }

    /**
     * 聊天消息已读.
     */
    #[Middlewares([MessageMiddleware::class])]
    #[RequestMapping(path: 'read', methods: 'put')]
    public function read()
    {
        $params = $this->request->all();
        $this->validate($params, [
            'to_user' => 'required',
            'is_group' => 'required',
        ]);
        $this->messageService->read($params);
        return $this->success([], $this->request);
    }

    /**
     * 聊天消息撤回.
     */
    #[RequestMapping(path: 'undo', methods: 'put')]
    public function undo()
    {
        $params = $this->request->all();
        $this->validate($params, [
            'id' => 'required',
        ]);
        $this->messageService->undo($params);
        return $this->success([], $this->request);
    }

    /**
     * 未读聊天消息.
     */
    #[RequestMapping(path: 'unread', methods: 'get')]
    public function unread()
    {
        $data = $this->messageService->unread(auth()->user()->id);
        return $this->success($data, $this->request);
    }
}
